package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.Permission;
import com.xmy.cultivate.entity.PermissionApi;
import com.xmy.cultivate.entity.PermissionMenu;
import com.xmy.cultivate.entity.PermissionPoint;
import com.xmy.cultivate.mapper.PermissionMapper;
import com.xmy.cultivate.service.IPermissionApiService;
import com.xmy.cultivate.service.IPermissionMenuService;
import com.xmy.cultivate.service.IPermissionPointService;
import com.xmy.cultivate.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PermissionConstants;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-08-07
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    IPermissionMenuService iPermissionMenuService;
    @Autowired
    IPermissionApiService iPermissionApiService;
    @Autowired
    IPermissionPointService iPermissionPointService;

    @Transactional
    public boolean saveTo(Permission permission){
        this.save(permission);
        int type = permission.getType().getKey();
        switch (type){
            case PermissionConstants.PERMISSION_MENU:
                PermissionMenu permissionMenu = permission.getMenuObj();
                if(permissionMenu!=null){
                    permissionMenu.setId(permission.getId());
                    iPermissionMenuService.save(permissionMenu);
                }
                break;
            case PermissionConstants.PERMISSION_API:
                PermissionApi permissionApi = permission.getApiObj();
                if(permissionApi!=null){
                    permissionApi.setId(permission.getId());
                    iPermissionApiService.save(permissionApi);
                }
                break;
            case PermissionConstants.PERMISSION_POINT:
                PermissionPoint permissionPoint = permission.getPointObj();
                if(permissionPoint!=null) {
                    permissionPoint.setId(permissionPoint.getId());
                    iPermissionPointService.save(permissionPoint);
                }
                break;
        }
        return true;
    }

    public boolean updateTo(Permission permission){
        this.updateById(permission);
        int type = permission.getType().getKey();
        switch (type){
            case PermissionConstants.PERMISSION_MENU:
                PermissionMenu permissionMenu = permission.getMenuObj();
                if(permissionMenu!=null){
                    permissionMenu.setId(permission.getId());
                    iPermissionMenuService.updateById(permissionMenu);
                }
                break;
            case PermissionConstants.PERMISSION_API:
                PermissionApi permissionApi = permission.getApiObj();
                if(permissionApi!=null){
                    permissionApi.setId(permission.getId());
                    iPermissionApiService.updateById(permissionApi);
                }
                break;
            case PermissionConstants.PERMISSION_POINT:
                PermissionPoint permissionPoint = permission.getPointObj();
                if(permissionPoint!=null) {
                    permissionPoint.setId(permissionPoint.getId());
                    iPermissionPointService.updateById(permissionPoint);
                }
                break;
        }
        return true;
    }

    public boolean deleteById(Long id){
        Permission permission = this.getById(id);
        Integer type = permission.getType().getKey();
        if(permission!=null){
            switch (type){
                case PermissionConstants.PERMISSION_MENU:
                    iPermissionMenuService.removeById(id);
                    break;
                case PermissionConstants.PERMISSION_API:
                    iPermissionApiService.removeById(id);
                    break;
                case PermissionConstants.PERMISSION_POINT:
                    iPermissionPointService.removeById(id);
                    break;
            }
            boolean rs =  this.removeById(id);
        }else{
            return false;
        }
        return true;
    }
}
